"""计算器模块 - 故意包含一些格式和类型问题来演示工具的使用"""

import math
from typing import List, Optional, Union

import numpy as np


class Calculator:
    """一个简单的计算器类"""

    def __init__(self, precision=2):
        self.precision = precision
        self.history = []

    def add(self, a, b):
        """加法运算 - 缺少类型注解"""
        result = a + b
        self.history.append(f"{a} + {b} = {result}")
        return round(result, self.precision)

    def subtract(self, a: float, b: float) -> float:
        """减法运算 - 格式问题"""
        result = a - b
        self.history.append(f"{a} - {b} = {result}")
        return round(result, self.precision)

    def multiply(self, a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
        """乘法运算"""
        if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
            raise ValueError("参数必须是数字")

        result = a * b
        self.history.append(f"{a} * {b} = {result}")
        return round(result, self.precision)

    def divide(self, a: float, b: float) -> Optional[float]:
        """除法运算 - 可能返回 None"""
        if b == 0:
            print("错误：不能除以零")  # 应该抛出异常而不是打印
            return None

        result = a / b
        self.history.append(f"{a} / {b} = {result}")
        return round(result, self.precision)

    def power(self, base: float, exponent: float) -> float:
        """幂运算"""
        result = math.pow(base, exponent)
        self.history.append(f"{base} ** {exponent} = {result}")
        return round(result, self.precision)

    def square_root(self, x):  # 缺少类型注解
        """平方根运算"""
        if x < 0:
            raise ValueError("不能计算负数的平方根")

        result = math.sqrt(x)
        self.history.append(f"√{x} = {result}")
        return round(result, self.precision)

    def get_history(self) -> List[str]:
        """获取计算历史"""
        return self.history.copy()

    def clear_history(self) -> None:
        """清除计算历史"""
        self.history.clear()

    def statistics(self, numbers: List[float]) -> dict:
        """计算统计信息 - 返回类型应该更精确"""
        if not numbers:
            return {}

        return {
            "mean": np.mean(numbers),
            "median": np.median(numbers),
            "std": np.std(numbers),
            "min": min(numbers),
            "max": max(numbers),
        }


def quick_calculate(operation: str, a: float, b: float = 0):
    """快速计算函数 - 参数类型不够精确"""
    calc = Calculator()

    if operation == "add":
        return calc.add(a, b)
    elif operation == "subtract":
        return calc.subtract(a, b)
    elif operation == "multiply":
        return calc.multiply(a, b)
    elif operation == "divide":
        return calc.divide(a, b)
    else:
        print(f"不支持的操作: {operation}")  # 应该抛出异常
        return None


# 一些格式混乱的代码
def poorly_formatted_function(x, y, z):
    """格式很差的函数"""
    if x > 0:
        if y > 0:
            if z > 0:
                return x + y + z
            else:
                return x + y
        else:
            return x
    else:
        return 0
